<template>
  <div>
    <el-tabs v-model="activeKey" @tab-click="handleClick">
      <el-tab-pane label="单笔退款" name="1"></el-tab-pane>
      <el-tab-pane label="批量退款" name="2"></el-tab-pane>
    </el-tabs>
    <el-card style="position: relative; min-width: 1200px; overflow: inherit">
      <div v-show="eventTab == '1'" class="tableSelectWrap">
        <el-form ref="refundForm" :model="commandForm" label-width="100px">
          <el-row>
            <el-col :span="7" style="display: flex">
              <el-form-item prop="dateType" label-width="0">
                <el-select
                  class="leftSelect"
                  v-model="commandForm.dateType"
                  style="width: 100px; border-radius: none"
                  placeholder="选择时间"
                >
                  <el-option label="创建时间" value="1"></el-option>
                  <!-- <el-option :label="'退款时间'" value="2"></el-option> -->
                </el-select>
              </el-form-item>
              <el-form-item prop="date" label-width="0" style="flex: 1">
                <el-date-picker
                  v-model="commandForm.date"
                  style="width: 100%; border-radius: 0px; margin-top: 1px"
                  type="datetimerange"
                  start-placeholder="开始时间"
                  :default-time="['00:00:00', '23:59:59']"
                  end-placeholder="结束时间"
                  :clearable="false"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1">
              <el-form-item label="退款状态" prop="refundStatus">
                <el-select
                  v-if="activeKey == '1'"
                  v-model="commandForm.refundStatus"
                  style="width: 100%"
                  placeholder="请选择退款状态"
                  clearable
                >
                  <el-option
                    v-for="item of refundStateList"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
                <el-select
                  v-if="activeKey == '2'"
                  v-model="commandForm.refundStatus"
                  style="width: 100%"
                  placeholder="请选择退款状态"
                  clearable
                >
                  <el-option
                    v-for="item of refundStateList1"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col
              :span="7"
              v-if="activeKey == '1'"
              :offset="1"
              :style="{ display: this.commandForm.payformChannel ? 'flex' : '' }"
            >
              <el-form-item label="支付渠道" prop="payformChannel">
                <el-select
                  v-model="commandForm.payformChannel"
                  class="leftSelect"
                  style="width: 100%"
                  placeholder="请选择支付渠道"
                  clearable
                  @change="handleChannel"
                >
                  <el-option
                    v-for="(item, index) of payChannel"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                label=""
                prop="payType"
                label-width="0"
                v-if="commandForm.payformChannel"
              >
                <el-select
                  class="rightSelect"
                  v-model="commandForm.payType"
                  style="width: 100%"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="(item, index) of payChannelTypeList"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col
              v-if="activeKey == '2'"
              :span="7"
              :offset="1"
              style="min-width: 170px"
            >
              <el-button class="width120" @click="this.resetFields"
                >重置</el-button
              >
              <el-button type="primary" class="width120" @click="()=>{
               this.dataForm=JSON.parse(JSON.stringify(this.commandForm));
               this.handleQuery()
              }"
                >查询</el-button
              >
            </el-col>
          </el-row>
          <el-row v-if="activeKey == '1'">
            <el-col :span="7" style="min-width: 170px">
              <el-button class="width120" @click="this.resetFields"
                >重置</el-button
              >
              <el-button type="primary" class="width120" @click="()=>{
               this.dataForm=JSON.parse(JSON.stringify(this.commandForm));
               this.handleQuery()
              }"
                >查询</el-button
              >
            </el-col>
          </el-row>
          <el-row>
            <el-button
              @click="exchangeEventTab"
              icon="el-icon-search"
              class="searchBtn"
              >精确搜索</el-button
            >
          </el-row>
        </el-form>
      </div>
      <div v-show="eventTab == '2'" class="tabWrap">
        <el-input
          class="search"
          clearable
          v-model="searchVal"
          :placeholder="
            activeKey == '1'
              ? '请输入退款订单号/退款流水号'
              : '请输入批量退款批次号'
          "
          @keyup.enter.native="()=>{
               this.activeKey=='1'?this.commandForm.refundNo=this.searchVal:this.commandForm.batchNo=this.searchVal
               this.dataForm=JSON.parse(JSON.stringify(this.commandForm));
               this.globalQuery()
              }"
        >
          <!-- <i slot="prefix" class="el-input__icon el-icon-search-outlined"></i> -->
        </el-input>
        <div class="searBtn">
          <el-button type="primary" @click="()=>{
            this.activeKey=='1'?this.commandForm.refundNo=this.searchVal:this.commandForm.batchNo=this.searchVal
               this.dataForm=JSON.parse(JSON.stringify(this.commandForm));
               this.globalQuery()
              }">搜索</el-button>
        </div>
        <div class="backWrap" @click="exchangeEventTab">
          <span>筛选搜索</span>
          <img src="@/assets/imgs/setting/down1.png" />
        </div>
      </div>
    </el-card>

    <el-card
      class="textBox"
      style="min-width: 1200px; margin-bottom: -5px; margin-top: 15px"
    >
      <span class="numBox">
        <span>退款总额</span>
        <span class="symbol">￥</span>
        <span class="num">{{ info.fmtRefundTotalAmount || 0.0 }}</span>
      </span>
      <span class="numBox">
        <span>退款量</span>
        <span class="num symbol">{{ info.refundTotalCount || 0 }}</span>
        <span> 笔</span>
      </span>
      <span class="numBox">
        <span>已退款</span>
        <span class="symbol">￥</span>
        <span class="num">{{ info.fmtFinishRefundTotalAmount || 0.0 }}</span>
      </span>
    </el-card>

    <el-card style="margin-top: 20px; min-width: 1200px">
      <div class="switch">
        <div>
          <el-switch v-model="isFixedTableH" inactive-text="固定表头">
          </el-switch>
        </div>
      </div>
      <el-table
        v-if="activeKey == '1'"
        :data="dataList"
        :height="fixedTableHeight"
      >
        <template slot="empty">
          <el-empty
            :image="require('@/assets/imgs/middleWork/k1.png')"
            :image-size="80"
            description="暂无相关数据"
          ></el-empty>
        </template>
        <el-table-column
          show-overflow-tooltip
          prop="gmtCreated"
          align="center"
          label="创建时间"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.gmtCreated ? scope.row.gmtCreated : "-" }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column show-overflow-tooltip prop="refundTime" align="center" label="退款时间">
            <template slot-scope="scope">
            <div style="overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
              {{scope.row.refundTime?scope.row.refundTime:'-'}}
            </div>
          </template>
          </el-table-column> -->
        <!-- <el-table-column show-overflow-tooltip prop="refundNo" align="center" label="商户退款订单号">
            <template slot-scope="scope">
            <div style="overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
              {{scope.row.refundNo?scope.row.refundNo:'-'}}
            </div>
          </template>
          </el-table-column> -->
        <el-table-column
          show-overflow-tooltip
          prop="refundNo"
          align="center"
          label="退款订单号"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.refundNo ? scope.row.refundNo : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtRefundAmount"
          align="center"
          label="退款金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{
                scope.row.fmtRefundAmount ? scope.row.fmtRefundAmount : "0.00"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtRefundFeeAmount"
          align="center"
          label="退款手续费/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{
                scope.row.fmtRefundFeeAmount
                  ? scope.row.fmtRefundFeeAmount
                  : "0.00"
              }}
            </div>
          </template>
        </el-table-column>

        <el-table-column
          show-overflow-tooltip
          prop="channelPayFormTypeName"
          align="center"
          label="支付渠道"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              <span>{{
                scope.row.channelPayFormTypeName
                  ? scope.row.channelPayFormTypeName
                  : "-"
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="refundStatus"
          align="center"
          label="退款状态"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              <span :class="setRefundStatus(scope.row.refundStatus).color">{{
                setRefundStatus(scope.row.refundStatus).str
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="address"
          align="center"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="handleDetails(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-table
        v-if="activeKey == '2'"
        :data="dataList"
        :height="fixedTableHeight"
      >
        <template slot="empty">
          <el-empty
            :image="require('@/assets/imgs/middleWork/k1.png')"
            :image-size="80"
            description="暂无相关数据"
          ></el-empty>
        </template>
        <el-table-column
          show-overflow-tooltip
          prop="gmtCreated"
          align="center"
          label="创建时间"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.gmtCreated ? scope.row.gmtCreated : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="batchNo"
          align="center"
          label="退款批次号"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.batchNo ? scope.row.batchNo : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtTotalRefundAmount"
          align="center"
          label="退款总金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{
                scope.row.fmtTotalRefundAmount
                  ? scope.row.fmtTotalRefundAmount
                  : "0.00"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtTotalRefundFeeAmount"
          align="center"
          label="退款总手续费/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{
                scope.row.fmtTotalRefundFeeAmount
                  ? scope.row.fmtTotalRefundFeeAmount
                  : "0.00"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="orderCount"
          align="center"
          label="退款总笔数/笔"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              <span>{{
                scope.row.orderCount ? scope.row.orderCount : "-"
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="refundStatus"
          align="center"
          label="退款状态"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              <span :class="setRefundStatus1(scope.row.refundStatus).color">{{
                setRefundStatus1(scope.row.refundStatus).str
              }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="address"
          align="center"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="handleDetails(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      layout="prev, pager, next, sizes, jumper"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :current-page="pageNum"
      :total="total"
      @current-change="pageCurrentChangeHandle"
      @size-change="pageSizeChangeHandle"
    >
    </el-pagination>
    <!-- 详情抽屉 -->
    <detail-drawer
      :drawer.sync="drawer"
      :info="detailsInfo"
      :activeKey="activeKey"
    ></detail-drawer>
  </div>
</template>

<script>
import detailDrawer from "./detailDrawer.vue";
import { setTabIndex, getTabIndex } from "@/utils/public";
import mixin from "@/mixins/mixin";
export default {
  mixins: [mixin],
  name: "refundTool",
  components: {
    detailDrawer,
  },
  data() {
    return {
      activeKey: getTabIndex(),
      mixinViewModuleOptions: {
        getDataListURL: "/api/orderInfo/searchRefundOrderByPage",
        createdIsNeed: false,
      },
      dataList: [],
      detailsInfo: {},
      drawer: false,
      commandForm: {
        onlyShowSelft: "",
        dateType: "1",
        date: [
          this.$moment().startOf("day").format("YYYY-MM-DD HH:mm:ss"),
          this.$moment().endOf("day").format("YYYY-MM-DD HH:mm:ss"),
        ],
        gmtCreatedBegin: this.$moment()
          .startOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
        gmtCreatedEnd: this.$moment()
          .endOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
        refundStatus: "",
        payformChannel: "",
        payType: "",
      },
      searchVal: "", // 搜索框的值
      form: {},
      refundStateList: [
        { id: 1, label: "所有", value: "" },
        { id: 2, label: "待退款", value: "wait_refund" },
        { id: 3, label: "退款成功", value: "success_refund" },
        { id: 4, label: "退款失败", value: "fail_refund" },
      ],
      refundStateList1: [
        { id: 1, label: "所有", value: "" },
        { id: 2, label: "处理中", value: "wait_refund" },
        { id: 3, label: "退款完成", value: "success_refund" },
      ],
      payChannel: [],
      // payChannel: this.$store.state.channelList,
      payChannelType: this.$store.state.channelListType,
      payChannelTypeList: [],
      info: {},
      eventTab: "1",
    };
  },
  created() {
    if (!this.commandForm.payformChannel) {
      this.payChannelTypeList = [{ label: "所有", value: "" }];
    }
    this.getChannelList();
  },
  mounted() {
    this.handleClick();
  },
  watch: {
    "$store.state.testLine": {
      handler() {
        if (this.$route.name == this.$options.name) {
          if(this.eventTab=='1'){
              this.handleQuery();
            }else if(this.eventTab=='2'){
              this.globalQuery();
            }
        }
      },
    },
    deep: true,
  },
  methods: {
    getTabIndex,
    getChannelList() {
      this.$http.get("/api/dict/channelList").then((res) => {
        if (res.code === "0000") {
          var a = [{ label: "所有", value: "" }];
          var b = a.concat(res.data);
          this.payChannel = b;
        }
      });
    },
    exchangeEventTab() {
      if (this.eventTab == "1") {
        this.eventTab = "2";
        if (this.activeKey == "1") {
        this.commandForm = {
          refundNo: this.searchVal,
          onlyShowSelf: true,
        };
      } else {
        this.commandForm = {
          batchNo: this.searchVal,
          onlyShowSelf: true,
        };
      }
    this.dataForm=JSON.parse(JSON.stringify(this.commandForm))
        this.globalQuery();
      } else {
        this.eventTab = "1";
        this.commandForm = {
        onlyShowSelft: "",
        dateType: "1",
        date: [
          this.$moment().startOf("day").format("YYYY-MM-DD HH:mm:ss"),
          this.$moment().endOf("day").format("YYYY-MM-DD HH:mm:ss"),
        ],
        gmtCreatedBegin: this.$moment()
          .startOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
        gmtCreatedEnd: this.$moment()
          .endOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
        refundStatus: "",
        payformChannel: "",
        payType: "",
      };
    this.dataForm=JSON.parse(JSON.stringify(this.commandForm))
        this.handleQuery();
      }
    },
    //重置操作
    resetFields() {
      this.resetForm("refundForm");
      // this.commandForm = {
      //   onlyShowSelft: "",
      //   dateType: "1",
      //   date: [],
      //   refundStatus: "",
      //   payformChannel: "",
      //   payType: "",
      // };
    },
    handleDetails(row) {
      switch (this.activeKey) {
        case "1":
          this.$http
            .get(`/api/orderInfo/getRefundById?id=${row.id}`)
            .then((res) => {
              if (res.code === "0000") {
                this.detailsInfo = res.data;
                this.drawer = true;
              }
            });
          break;
        case "2":
          this.$http
            .get(
              `/api/orderInfo/listOrderByRefundBatchId?batchId=${row.batchNo}`
            )
            .then((res) => {
              if (res.code === "0000") {
                this.detailsInfo = res.data;
                this.drawer = true;
              }
            });
          break;
        default:
          break;
      }
    },
    setRefundStatus(val) {
      let str = "";
      let color = "";
      switch (val) {
        case "part_refund":
          str = "部分成功";
          color = "succeed";
          break;
        case "wait_refund":
          str = "待退款";
          color = "pedding";
          break;
        case "success_refund":
          str = "退款成功";
          color = "succeed";
          break;
        case "fail_refund":
          str = "退款失败";
          color = "failed";
          break;
      }
      return {
        str,
        color,
      };
    },
    setRefundStatus1(val) {
      let str = "";
      let color = "";
      switch (val) {
        case "wait_refund":
          str = "处理中";
          color = "pedding";
          break;
        case "success_refund":
          str = "退款完成";
          color = "succeed";
          break;
      }
      return {
        str,
        color,
      };
    },
    handleClick() {
      setTabIndex(this.activeKey);
      this.pageSize = 10;
      this.resetForm("refundForm");
      this.payChannelTypeList = [];
      this.commandForm.date = [
        this.$moment().startOf("day").format("YYYY-MM-DD HH:mm:ss"),
        this.$moment().endOf("day").format("YYYY-MM-DD HH:mm:ss"),
      ];
      this.dataForm=JSON.parse(JSON.stringify(this.commandForm))
      this.handleQuery();
    },
    handleQuery() {
      this.eventTab = "1";
      this.searchVal = '';
      this.dataForm.gmtCreatedBegin = undefined;
      this.dataForm.gmtCreatedEnd = undefined;
      this.dataForm.refundTimeBegin = undefined;
      this.dataForm.refundTimeEnd = undefined;
      if (this.dataForm.date && this.dataForm.date.length) {
        if (this.dataForm.dateType === "1") {
          this.dataForm.gmtCreatedBegin = this.dataForm.date[0];
          this.dataForm.gmtCreatedEnd = this.dataForm.date[1];
        } else {
          this.dataForm.refundTimeBegin = this.dataForm.date[0];
          this.dataForm.refundTimeEnd = this.dataForm.date[1];
        }
      }
      if (this.activeKey == "1") {
        this.mixinViewModuleOptions.getDataListURL =
          "/api/orderInfo/searchRefundOrderByPage";
        this.getStatisticsInfo();
      } else {
        this.mixinViewModuleOptions.getDataListURL =
          "/api/orderInfo/searchBatchRefundOrderByPage";
        this.getBatchStatisticsInfo();
      }
      this.getDataList();
    },
   globalQuery() {
      this.eventTab = "2";
      if (this.activeKey == "1") {
        this.getStatisticsInfo();
      } else {
        this.getBatchStatisticsInfo();
      }
      this.getDataList();
    },
    getStatisticsInfo() {
      this.$http
        .post("/api/orderInfo/refundStatistics", this.dataForm)
        .then((res) => {
          if (res.code === "0000") {
            this.info = res.data || {};
          }
        });
    },
    getBatchStatisticsInfo() {
      this.$http
        .post("/api/orderInfo/batchRefundStatistics", this.dataForm)
        .then((res) => {
          if (res.code === "0000") {
            this.info = res.data || {};
          }
        });
    },
    handleChannel(val) {
      if (val) {
        this.payChannelTypeList = this.payChannelType[val];
      } else {
        this.payChannelTypeList = [];
      }
      this.commandForm.payType = "";
    },
  },
};
</script>

<style scoped lang="scss">
.tabWrap {
  // height: 210px;
  height: 133px;
  // background: #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  .search {
    // position: absolute;
    // top: -60px;
    // right: 0;
    width: 466px;
  }
  .backWrap {
    height: 20px;
    position: absolute;
    bottom: 8px;
    right: 10px;
    display: flex;
    align-items: center;
    cursor: pointer;
    span {
      display: inline-block;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #03bfc6;
      margin-right: 10px;
    }
  }
}
.searchBtn {
  margin-top: 18px;
  background: #f5f5f5;
}
.title {
  color: #4f595f;
  position: relative;
  font-size: 18px;
  font-weight: 500;
  line-height: 34px;
  margin-left: 10px;
}
.textBox {
  display: flex;
  .label {
    color: #869daa;
    font-size: 14px;
    width: 120px;
    margin-left: 10px;
    text-align: right;
  }
  .val {
    font-size: 14px;
    margin-left: 20px;
    color: #869daa;
    width: 700px;
  }
}
.pedding {
  color: #f69c1f;
}
.succeed {
  color: #07a555;
}
.failed {
  color: #f42b2b;
}
.switch {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
}
// .search {
//   position: absolute;
//   right: 0;
//   top: -164px;
// }
.ml {
  margin-left: 20px;
}
:deep(.leftSelect) {
  .el-input__inner {
    border-radius: 4px 0 0 4px;
  }
}
:deep(.search) {
  .el-input__inner {
    border-radius: 2px 0 0 2px;
    height: 38px;
    line-height: 38px;
  }
}
:deep(.searBtn) {
  .el-button--small {
    border-radius: 0 2px 2px 0;
    padding: 12px 24px;
  }
}
</style>
